/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.panel;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jxSwing.component.button.OpenButton;
import org.jxSwing.component.chooser.DirectoryChooser;
import org.jxSwing.component.chooser.FileChooser;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.panel
 * @date 18/09/2014 - 18:49:02
 */
public class FileChooserPanel extends JPanel {
	
	private static final long serialVersionUID = -5694894271515343707L;
	
	private File file;
	
	private final ChooseMode chooseMode;
	
	private JTextField txtFile;
	
	private OpenButton btnOpen;
	
	/**
	 * 
	 */
	public FileChooserPanel() {
	
		this(ChooseMode.ONLY_DIRECTORY);
	}
	
	/**
	 * @param chooseMode
	 */
	public FileChooserPanel(final ChooseMode chooseMode) {
	
		this.chooseMode = chooseMode;
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		final GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{150, 30, 0};
		gridBagLayout.rowHeights = new int[]{25, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		
		setLayout(gridBagLayout);
		
		final GridBagConstraints gbc_txtFile = new GridBagConstraints();
		gbc_txtFile.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtFile.insets = new Insets(0, 0, 0, 10);
		gbc_txtFile.gridx = 0;
		gbc_txtFile.gridy = 0;
		
		add(getTxtFile(), gbc_txtFile);
		
		final GridBagConstraints gbc_btnOpen = new GridBagConstraints();
		gbc_btnOpen.gridx = 1;
		gbc_btnOpen.gridy = 0;
		
		add(getBtnOpen(), gbc_btnOpen);
	}
	
	/**
	 * @return the btnOpen
	 */
	public OpenButton getBtnOpen() {
	
		if(btnOpen == null) {
			btnOpen = new OpenButton();
			btnOpen.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(final ActionEvent actionEvent) {
				
					JFileChooser chooser = null;
					
					//
					if(chooseMode == ChooseMode.ONLY_DIRECTORY) {
						
						chooser = new DirectoryChooser();
						
					}else if(chooseMode == ChooseMode.ONLY_FILE) {
						
						chooser = new FileChooser();
						
					}else {
						
						chooser = new JFileChooser();
					}
					
					//
					chooser.setMultiSelectionEnabled(false);
					
					//
					if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
						
						file = chooser.getSelectedFile();
						
						getTxtFile().setText(file.getAbsolutePath());
					}
				}
			});
		}
		return btnOpen;
	}
	
	/**
	 * @return the file
	 */
	public final File getFile() {
	
		return file;
	}
	
	/**
	 * @return the txtFile
	 */
	public JTextField getTxtFile() {
	
		if(txtFile == null) {
			txtFile = new JTextField();
			txtFile.setEnabled(false);
		}
		return txtFile;
	}
	
	/**
	 * @author Denilson Edinaldo Pinto
	 * @porject jxSwing
	 * @package org.jxSwing.component.panel
	 * @date 18/09/2014 - 19:00:13
	 */
	public enum ChooseMode {
		DIRECTORY_AND_FILE,
		ONLY_DIRECTORY,
		ONLY_FILE
	}
}
